package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.MemberInfo;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RequestMapping("/menu")
@RestController
public class MenuController {
    @Reference
    private MenuService menuService;;

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        Integer id = menu.getId();
        try {
            if (id == null){
                    menuService.add(menu);
                }else{
                menuService.Update(menu);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"操作失败");
        }
        return new Result(true,"操作成功");
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = menuService.finpage(queryPageBean);
        return pageResult;
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,"查询成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try {
            menuService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
        return new Result(true,"删除成功");
    }
    @RequestMapping("/findLala")
    public Result findLala(){
        try {
            List<Integer> list = menuService.findLala();
            return new Result(true,"成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }
    }


    //批量导出
     @RequestMapping("/excelDownload")
    public void batchDownload(Integer[] ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //根据文件的路径获取编译后的路径
        //获取该文件的服务器访问路径（这里需要传入webapp下，你文件的路径，生成服务器访问路径）

         List<MemberInfo> list = menuService.findByIds(ids);
        System.out.println(list);
        //获得Excel模板文件绝对路径
        String templateRealPath = request.getSession().getServletContext().getRealPath("template") +
                File.separator + "batchDownload.xlsx";
        //读取模板文件创建Excel表格对象
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(templateRealPath)));
        XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFRow row = null;

        int rowNum = 1;
        for (MemberInfo memberInfo : list) {
            row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(memberInfo.getName());
            row.createCell(1).setCellValue(memberInfo.getSex());//性别
            row.createCell(2).setCellValue(memberInfo.getPhoneNumber());//手机号
            row.createCell(3).setCellValue(memberInfo.getAddress());//地址
            row.createCell(4).setCellValue(memberInfo.getSetmealName());//套餐
            row.createCell(5).setCellValue(memberInfo.getCgName().toString());//检查组
            row.createCell(6).setCellValue(memberInfo.getCkName().toString());//检查项
        }
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
         UUID uuid = UUID.randomUUID();
         response.setHeader("content-Disposition", "attachment;filename="+uuid.toString()+"aaa.xlsx");
        workbook.write(outputStream);

        outputStream.flush();
        outputStream.close();
        workbook.close();

    }

}
